/*
 * @Description  : 
 * @Author       : 陈韵同
 * @Date         : 2021-03-11 14:34:10
 * @LastEditTime : 2021-03-18 15:21:26
 * @LastEditors  : 陈韵同
 * @FilePath     : e:\协同云组件库\新建文件夹 (2)\cloud-components\tests\utils\demoTest.js
 */
import glob from 'glob';
import { mount, createLocalVue, RouterLinkStub } from '@vue/test-utils';
import MockDate from 'mockdate';
import moment from 'moment';
import UI from '../../packages/index';
const localVue = createLocalVue()
localVue.use(UI)

const sleep = (timeout = 0) => new Promise(resolve => setTimeout(resolve, timeout));

const getComponent = (file) => {
  const demo = require(`../.${file}`).default || require(`../.${file}`);
  return demo;
}

export default function demoTest(component, options = {}) {
  const suffix = options.suffix || 'vue';
  const files = glob.sync(`./docs/pages/components/${component}/*.${suffix}`);

  files.forEach(file => {
    let testMethod = options.skip === true ? test.skip : test;
    if (Array.isArray(options.skip) && options.skip.some(c => file.includes(c))) {
      testMethod = test.skip;
    }
    testMethod(`renders ${file} correctly`, async () => {
      MockDate.set(moment('2016-11-22'));
      const demo = require(`../.${file}`).default || require(`../.${file}`);
      document.body.innerHTML = '';
      const wrapper = mount(demo, {
        localVue,
        stubs: {
          // 需要<router-link>
          RouterLink: RouterLinkStub
        },
        sync: false,
        attachTo: document.getElementById('root')
      });
      await sleep();
      const dom = options.getDomFromElement ? wrapper.element : wrapper.html();
      expect(dom).toMatchSnapshot();
      MockDate.reset();
      wrapper.destroy();
      document.body.innerHTML = '';
    });
  });
}

export {
  getComponent,
  sleep
}
